﻿/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DaveSexton.DocToMaml
{
	internal sealed class ProjectManager : ProjectManagerBase<Project>
	{
		#region Public Properties
		public InputCollection Inputs
		{
			get
			{
				return inputs;
			}
		}

		public ConceptualImageManager Images
		{
			get
			{
				return images;
			}
		}

		public RuleContainer Rules
		{
			get
			{
				return rules;
			}
		}

		public string LastOutputDirectory
		{
			get
			{
				return Project.LastOutputDirectory;
			}
		}

		public override string DefaultFileExtension
		{
			get
			{
				return ".d2m";
			}
		}
		#endregion

		#region Private / Protected
		private readonly InputCollection inputs = new InputCollection();
		private readonly RuleContainer rules = new RuleContainer();
		private readonly ConceptualImageManager images = new ConceptualImageManager();
		private readonly FileGuidManager guids = new FileGuidManager();
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ProjectManager" /> class.
		/// </summary>
		public ProjectManager(ProgramForm owner, IServiceProvider services)
			: base(owner, services)
		{
		}
		#endregion

		#region Methods
		protected override Project CreateProject(string file)
		{
			Project project = new Project(file, inputs, rules, images, guids);

			project.StateChanged += (o, e) => IsDirty = true;

			return project;
		}

		public FileInput CreateFileInput(string file)
		{
			return Project.CreateFileInput(file);
		}

		public FolderInput CreateFolderInput(string path)
		{
			return Project.CreateFolderInput(path);
		}

		public void EnsureInputGuids()
		{
			guids.EnsureGuids(inputs);
		}
		#endregion

		#region Event Handlers
		protected override void OnOpened(ProjectOpenedEventArgs e)
		{
			if (e.IsNew)
			{
				rules.Add(new RemoveRule("script", RuleMatchKind.TagName));
				rules.Add(new RemoveRule("header", RuleMatchKind.ElementId));
				rules.Add(new RemoveRule("footer", RuleMatchKind.ElementId));
				rules.Add(new RemoveRule("header", RuleMatchKind.StyleSheetClass));
				rules.Add(new RemoveRule("footer", RuleMatchKind.StyleSheetClass));
				rules.Add(new ConvertRule("h1", RuleMatchKind.TagName, Maml.MamlElementType.Section));
				rules.Add(new ConvertRule("h2", RuleMatchKind.TagName, Maml.MamlElementType.Section));
			}

			base.OnOpened(e);
		}

		protected override void OnClosed(EventArgs e)
		{
			rules.Clear();
			images.Clear();
			guids.Clear();

			foreach (IInput input in inputs)
				input.Dispose();

			inputs.Clear();

			base.OnClosed(e);
		}
		#endregion
	}
}